import threading
import socket
import time

from core.server import EOM

class Client:
    def __init__(self):
        self.socket = socket.create_connection(("127.0.0.1",8888))
        threading.Thread(target=self.input,args=()).start()
        threading.Thread(target=self.recv,args=()).start()

    def input(self):
        while True:
            content = input("input:")
            self.send(content)

    def recv(self):
        while True:
            content=""
            while True:
                try:
                    content += self.socket.recv(1024).decode("utf-8")
                except:
                    break
                if content == "" or content.endswith(EOM):
                    break
            if content == "":
                break
            print(content)
        self.socket.close()

    def send(self,content: str):
        self.socket.send((content+EOM).encode("utf-8"))

    def exit(self):
        self.socket.close()

if __name__ == "__main__":
    c = Client()
    time.sleep(10)
    c.exit()
